package com.jtlrm.ckd.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.sdk.base.net.CommonObserver;
import com.base.sdk.util.ImageUtil;
import com.base.sdk.util.PhoneUtil;
import com.base.sdk.util.TuoMinUtil;
import com.base.sdk.util.ValidateUtils;
import com.hyphenate.chatuidemo.Constant;
import com.hyphenate.chatuidemo.ui.ChatActivity;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.acitvity.TitleBarActivity;
import com.jtlrm.ckd.entity.HuanZheBasicDataEntity;
import com.jtlrm.ckd.entity.event.RemarkUpdate;
import com.jtlrm.ckd.mvp.model.HuanZheGuanLiModel;
import com.jtlrm.ckd.util.EventBusUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * 聊天个人信息
 */
@Route(path = "/person/info/chat")
public class ChatPersonInfoActivity extends TitleBarActivity {
    String userId;
    HuanZheGuanLiModel huanZheGuanLiModel;
    @BindView(R.id.avatar)
    ImageView avatartI;
    @BindView(R.id.name)
    TextView nameT;
    @BindView(R.id.age)
    TextView ageT;
    @BindView(R.id.sex)
    TextView sexT;
    @BindView(R.id.phone)
    TextView phoneT;
    @BindView(R.id.id_card)
    TextView idCardT;
    @BindView(R.id.address)
    TextView addressT;
    @BindView(R.id.bao_xiao_style)
    TextView baoXiaoT;
    @BindView(R.id.education)
    TextView educationT;
    @BindView(R.id.yuanfabing)
    TextView yuanFaBingT;
    @BindView(R.id.bansuibing)
    TextView banSuiBingT;
    @BindView(R.id.zhiliao_style)
    TextView zhiLiaoT;
    @BindView(R.id.remark)
    TextView remarkT;
    HuanZheBasicDataEntity huanZheBasicDataEntity;
    String imId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected String getTitleText() {
        return "详细资料";
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_chat_person_info);
        ARouter.getInstance().inject(this);
    }

    @Override
    protected void initView() {
        userId = getIntent().getStringExtra("userId");
        imId = getIntent().getStringExtra("imId");
        huanZheGuanLiModel = new HuanZheGuanLiModel();
        EventBusUtil.register(this);
    }

    @Override
    protected void obtainData() {
        loadData();
    }

    private void loadData() {
        huanZheGuanLiModel.queryHuanZheBasic(userId, new CommonObserver<HuanZheBasicDataEntity>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
            }

            @Override
            public void onResult(HuanZheBasicDataEntity data) {
                huanZheBasicDataEntity = data;
                if (data != null) {
                    ImageUtil.loadCircleAvatarImage(avatartI, data.getHeadPicture());
                    setText(nameT, data.getName());
                    setText(ageT, data.getAge() + "岁");
                    setText(sexT, data.getSex());
                    setText(phoneT, data.getMobile());
                    setText(idCardT, TuoMinUtil.idEncrypt(data.getCardNo()));
                    setText(addressT, data.getFullAddress());
                    setText(baoXiaoT, data.getExpenseType());
                    setText(educationT, data.getEducation());
                    setText(yuanFaBingT, data.getOrgDisease());
                    setText(banSuiBingT, data.getWithDisease());
                    setText(zhiLiaoT, data.getMainTreatment());
                    setText(remarkT, data.getRemarkCkd());
                }
            }
        }, lifecycleSubject);
    }

    @Override
    protected void initEvent() {

    }

    public void callPhone(View view) {
        if (notEmpty(getTextStr(phoneT)) && ValidateUtils.Mobile(getTextStr(phoneT))) {
            PhoneUtil.diallPhone(context, getTextStr(phoneT));
        }
    }

    public void goManager(View view) {
        HuanZheManagerActivity.goActivity(context, userId);
    }

    public void goChat(View view) {
        Intent intent = new Intent(getActivity(), ChatActivity.class);
        intent.putExtra(Constant.EXTRA_USER_ID, imId);
        startActivity(intent);
    }

    public void clickRemark(View view) {
        RemarkActivity.goAcitvity(context, userId, true, huanZheBasicDataEntity.getRemarkCkd());
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RemarkUpdate message) {
        if (message != null && ("" + message.getUserId()).equals(userId)) {
            loadData();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregister(this);
    }
}
